package com.missilike.api.controller;

import com.missilike.biz.IMilPhotoService;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.BizResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.service.IOSSServcice;
import com.missilike.core.utils.ResultUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.rmi.runtime.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2018-12-11 22:48
 **/
@Slf4j
@RestController
@RequestMapping("/api/oss")
public class OSSController {

    private IOSSServcice iossServcice;
    private IMilPhotoService milPhotoService;

    @Autowired
    public OSSController(IOSSServcice iossServcice,IMilPhotoService milPhotoService) {
        this.iossServcice = iossServcice;
        this.milPhotoService = milPhotoService;
    }

    @ApiOperation(value = "imagesUpload", notes = "上传图片")
    @RequestMapping(value = "/imagesUpload", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult imagesUpload(MultipartFile file) {
        //设置图片最大10M
        final long maxSize = 10240 * 10240;
        if (file.getSize() > maxSize) {
            return ResultUtils.error(90000,"上传图片大小不能超过10M！");
        }
        log.info("Start to execute /oss/imagesUpload body request={}", file);
        String fileName = iossServcice.updateImages(file);
        log.info("Start to execute /oss/imagesUpload responese={}", fileName);
        return ResultUtils.ok(fileName);
    }

    @ApiOperation(value = "pictureUpload", notes = "上传图片(返回图片id和url)")
    @RequestMapping(value = "/pictureUpload", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult pictureUpload(MultipartFile file){
        CommonResult result = imagesUpload(file);
        Map<String, Object> map = new HashMap<>(16);
        if (BaseResultCode.SUCCESS.getCode() == result.getCode()){
            Long id = milPhotoService.insertPhotoUrl(result.getData().toString());
            map.put("id", id);
            map.put("url", result.getData());
            result.setData(map);
        }
        return result;
    }

    @ApiOperation(value = "getPictureUrls", notes = "根据ids获取图片地址")
    @RequestMapping(value = "/getPictureUrls", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getPictureUrls(@RequestParam Long[] ids){
        List<Map<String, Object>> urls = new ArrayList<>();
        for (Long id : ids) {
            Map<String, Object> map = new HashMap<>(16);
            String url = milPhotoService.getPhotoUrl(id);
            map.put("id", id);
            map.put("url", url);
            urls.add(map);
        }
        return ResultUtils.ok(urls);
    }

    @ApiOperation(value = "picturesUpload", notes = "上传图片批量(返回图片集合)")
    @RequestMapping(value = "/picturesUpload", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult picturesUpload(@RequestParam("files") MultipartFile files[]) {
        CommonResult commonResult = ResultUtils.ok();
        List<Map<String, Object>> urls = new ArrayList<>();
        int successCount = 0;
        int failCount = 0;
        for (MultipartFile file : files) {
            CommonResult result = pictureUpload(file);
            if (result.getCode() == BaseResultCode.SUCCESS.getCode()) {
                Map<String, Object> map = (Map<String, Object>) result.getData();
                urls.add(map);
                successCount++;
            } else {
                failCount++;
            }
        }
        String massage = "成功上传" + successCount + "张图片，失败" + failCount + "张";
        commonResult.setMessage(massage);
        commonResult.setData(urls);
        return commonResult;
    }
}
